<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 放置标题图片 @是设置的别名 -->
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="" />
        </h3>
      </div>
      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="Mobile"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button
        class="login_btn"
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >
        登录
      </el-button>

      <div class="tips">
        <span style="margin-right: 20px">手机号: 13800000002</span>
        <span> 密码: 123456</span>
      </div>
      <el-button @click="testFn">测试</el-button>
    </el-form>
  </div>
</template>

<script>
import { validMobile } from '@/utils/validate' // 拿到的是Boolean值
import { getUserProfileAPI } from '@/api' // login请求
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    const validateMobile = (rule, value, callback) => {
      // 单独校验用户名
      if (!validMobile(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginRules: {
        mobile: [{ required: true, trigger: 'blur', validator: validateMobile }],
        password: [{ required: true, trigger: 'blur', min: 6, max: 16, message: '密码长度6-16为' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    // 监听路由的变化
    $route: {
      // 检测路由对象的改变(路由页面跳转这里就出发)
      handler: function (route) {
        // rpute相当于this.$route(路由信息对象)
        // 主动退出和被动推出,/login?redire=fullPath地址
        this.redirect = route.query && route.query.redirect // 拿到未遂地址
        // 尾随地址:退出登陆所在页面路由完整路径
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions('user', ['loginActions']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // this
      this.$refs.loginForm.validate(async valid => {
        // 校验整个表单
        if (valid) {
          // 箭头函数的this找外面
          this.loading = true // 加载节流阀，加载中转圈
          // 知识点1:await只能拿到成功结果,如果promise对象内用reject()返回,这里就终端不往下执行
          // 知识点2:当Promise对象这里,没有自己捕获到错误(try+catch,或者.catch())然后处理的话,浏览器就会把"抛出的这个错误"打印到控制台里,"终止代码继续往下"
          // 知识点3:捕获promise的错误由2种方式
          // 通用:try+catch
          // 只针对promise的:await Promise对象.catch(err=>err)
          // const res = await LoginAPI(this.loginForm).catch(err=>console.rooer(err))
          // 弹窗element-ui的$message
          // await处理错误信息
          // 左边 awaitTo捕获错误处理返回值 = 右边 请求头和actions处理的返回值
          const [err, res] = await this.loginActions(this.loginForm)
          // console.log(res)只能获取用户登录信息
          if (err) this.$message.error(err.message)
          else {
            this.$store.commit('user/SET_TOKEN', res.data)
            this.$router.replace(this.redirect || '/') // 登陆后跳转布局页面,同时重定向到仪表盘dashboard页面
            this.$message.success(res.message)
          }
          this.loading = false // 关闭加载阀
          // this.$store.dispatch('user/login', this.loginForm)
        } else {
          console.log('表单校验失败')
          return false
        }
      })
    },
    async testFn() {
      const resp = await getUserProfileAPI()
      console.log(resp)
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe; // 将输入框颜色改成蓝色
$cursor: #68b0fe; // 将输入框光标改成蓝色

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url('~@/assets/common/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login_btn {
    background: #407ffe;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
  }
}
</style>
